import 'package:flutter/material.dart';
import 'package:flutter_demo2/base/plugin/common_native.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Stateful Clicker Counter',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Clicker Counter Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

  const MyHomePage({Key? key, required this.title}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  @override
  void initState() {
    debugPrint("---MyHomePage: initState");
    Future.delayed(const Duration(milliseconds: 500)).then((value) {
      commonNative.finishSplash();
    });
    super.initState();
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("---MyHomePage: build");
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: const TextStyle(fontSize: 25),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatButton(
        incrementCounter: _incrementCounter,
        count: _counter,
      ),
    );
  }
}

class FloatButton extends StatefulWidget {
  final VoidCallback? incrementCounter;
  final int count;

  FloatButton({Key? key, this.incrementCounter, required this.count}) : super(key: key) {
    debugPrint("---FloatButton: constructor call");
  }

  @override
  State<FloatButton> createState() {
    debugPrint("---FloatButton: createState");
    return _FloatButtonState();
  }

  @override
  StatefulElement createElement() {
    debugPrint("---FloatButton: createElement");
    return super.createElement();
  }
}

class _FloatButtonState extends State<FloatButton> {
  @override
  void initState() {
    debugPrint("---FloatButton: initState");
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("---FloatButton: build");
    return MaterialButton(
      onPressed: () {
        widget.incrementCounter?.call();
      },
      child: Text("counter == ${widget.count}"),
    );
  }
}
